#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Termstructures/Yield/RateHelper.h>
#pragma unmanaged 
#include <ql\termstructures\yield\bondhelpers.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL::Instruments;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Termstructures { namespace Yield {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IBondHelper
	public ref class CBondHelper  : 
            public CRateHelper,
            public Cephei::QL::Termstructures::Yield::IBondHelper
	{
	protected: 
		boost::shared_ptr<QuantLib::BondHelper>* _ppBondHelper;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::BondHelper>* _phBondHelper;
#endif
		Object^ _BondHelperOwner;     // reference to object that manages the storage for this object
	internal:
		CBondHelper (Cephei::QL::IQuote^ cleanPrice, Cephei::QL::Instruments::IBond^ bond);
        CBondHelper (boost::shared_ptr<QuantLib::BondHelper>& childNative, Object^ owner);
        CBondHelper (QuantLib::BondHelper& childNative, Object^ owner);
        CBondHelper (CBondHelper^ copy);
        CBondHelper (PLATFORM::Type^ t);
#ifdef STRUCT
        CBondHelper (QuantLib::BondHelper childNative);
#endif       
#ifdef HANDLE
		CBondHelper (QuantLib::Handle<QuantLib::BondHelper>& childNative, Object^ owner);
		CBondHelper (QuantLib::Handle<QuantLib::BondHelper> childNative);
#endif
		virtual ~CBondHelper ();
		!CBondHelper ();

	internal:
		QuantLib::BondHelper& GetReference ();
		boost::shared_ptr<QuantLib::BondHelper>& GetShared ();
		QuantLib::BondHelper* GetPointer ();
        void SetBondHelper (boost::shared_ptr<QuantLib::BondHelper> native)
        {
            if (_ppBondHelper != NULL)
                delete _ppBondHelper;
            _ppBondHelper = new boost::shared_ptr<QuantLib::BondHelper> (native);
            SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::BondHelper>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
        property Cephei::QL::Instruments::IBond^ Bond 
        {
		    virtual Cephei::QL::Instruments::IBond^ get () ;
        }
        property Double ImpliedQuote 
        {
		    virtual Double get () ;
        }
		virtual Cephei::QL::Termstructures::Yield::IBondHelper^ SetTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ t) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IBondHelper Vector interface
	ref class CBondHelperVector sealed : 
							    public Core::INativeVector<IBondHelper^>,
							    public Core::INativeVector<Core::Generic::ICoCell<IBondHelper^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::BondHelper>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::BondHelper>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::BondHelper>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CBondHelperVector ();
#ifdef STRUCT
		CBondHelperVector (std::vector<QuantLib::BondHelper>& vec, Object^ owner);
		CBondHelperVector (std::vector<QuantLib::BondHelper> vec);
#endif       
		CBondHelperVector (std::vector<boost::shared_ptr<QuantLib::BondHelper>>& vec, Object^ owner);
#ifdef HANDLE
		CBondHelperVector (std::vector<QuantLib::Handle<QuantLib::BondHelper>>& vec, Object^ owner);
#endif
		virtual ~CBondHelperVector ();
		!CBondHelperVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IBondHelper^ default[int] 
		{
			virtual IBondHelper^ get (int index) = Core::INativeVector<IBondHelper^>::default::get;
			virtual void set (int index, IBondHelper^ value) = Core::INativeVector<IBondHelper^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::Termstructures::Yield::IBondHelper^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<IBondHelper^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<IBondHelper^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<IBondHelper^>^ value) = Core::INativeVector<Core::Generic::ICoCell<IBondHelper^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::BondHelper>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::BondHelper>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::BondHelper>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Termstructures::Yield::IBondHelper^>::typeid)]
	[FactoryFor(Cephei::QL::Termstructures::Yield::IBondHelper::typeid)]
	[FactoryFor(Cephei::QL::Termstructures::Yield::IBondHelper_Factory::typeid)]
	public ref class CBondHelper_Factory sealed : public IBondHelper_Factory
	{
	public:
        virtual IBondHelper^ Create (Cephei::QL::IQuote^ cleanPrice, Cephei::QL::Instruments::IBond^ bond);
        /* <summary> Create a hybrid Vector of IBondHelper, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<IBondHelper^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IBondHelper, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IBondHelper^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<IBondHelper^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<IBondHelper^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IBondHelper^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<IBondHelper^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::Termstructures::Yield::IBondHelper^>^ CreateNativeVector ()
		{
			return REFNEW CBondHelperVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::Termstructures::Yield::IBondHelper^>^ CreateNativeMatrix ()
		{
			return nullptr;
		}

        virtual Core::INativeCube<Cephei::QL::Termstructures::Yield::IBondHelper^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL*/ } /*Termstructures*/ } /*Yield */}
